function personalInfo(){
	var $ul = $('#shezhi .wansnr ul');
	var useridTemp=window.localStorage.getItem("userid");
	var userkeyTemp=window.localStorage.getItem("userkey");
	if(useridTemp!=null&&userkeyTemp!=null){
		$.ajax({
			url :interfaceMobileUrl+"/userInfo.json",
			data:{'userid':useridTemp,'key':userkeyTemp},
			cache: false,
			jsonp: "callback",
			dataType: "jsonp",
			timeout: 3000,
			beforeSend:function(){
				$.ui.showMask("加载中..."); 
			},
			error:function(){
				$.ui.loadContent("#denglu",false,false,"up");
				xfAlert("加载个人信息失败，请重新登录");
			},
			complete:function(){
				$.ui.hideMask(); 
			},
			success:function(data){
				userid = useridTemp;
				userkey = userkeyTemp;
				$("#loginAndReg").css("display","none");
				$("#loginStatus").html("");
				$("#loginStatus").append("<a href='#'>"+
						"<img src='"+assetsUrl+"images/toux.jpg'>"+
						"<p class='yh_name'></p>"+
						"<p></p>"+
						"</a>" +
						"<a class='zc_sz' href='#shezhi'><img src="+assetsUrl+"'images/icon_sz.png' ></a>");
				
				
				var info = eval('(' + data + ')'); 
				info = info["com.opac.readerHome.domain.User"];
				libtied = info.libtied;
				mobile = info.mobile;
				defaultAddress = info.defaultAddress;
				$('#geren .dl_inner .yh_name').html(info.username);
//			if(info.email&&info.email){
//				
//			}else{
//				$('#geren .dl_inner .yh_name + p').html("");
//			}
				var $ul = $('#shezhi .wansnr ul');
				if(info.libtied){
					$ul.find('li').eq(0).addClass('cur').children('a').attr({'href':'javascript:void(0);'});
				}else{
					$ul.find('li').eq(0).removeClass('cur').children('a').attr({'href':'#bdduzz'});
				}
				if(info.mobileTied){
					$('#geren .dl_inner .yh_name + p').html("手机号:"+info.mobile);
					$ul.find('li').eq(1).addClass('cur').children('a').attr({'href':'javascript:void(0);'});
//				$ul.find('li').eq(1).addClass('cur').children('a').html(info.mobile).attr({'href':'javascript:void(0);','style':'padding-top: 20px;font-size: 25px;'});
				}else{
					$ul.find('li').eq(1).removeClass('cur').children('a').attr({'href':'#sjyz'});
					$('#geren .dl_inner .yh_name + p').html("");
				}
				if(info.addressSet){
					$ul.find('li').eq(2).addClass('cur').children('a').attr({'href':'javascript:void(0);'});
				}else{
					$ul.find('li').eq(2).removeClass('cur').children('a').attr({'href':'#huoyuedzStep1'});
				}
			}
		});
		
	}
}
function bdduzz_load() {
	bindRd();
}
function bdduzz_unload() {
	$.ui.hideMask();
}

//检查电话号码是否填写正确
checkPhone = function(_en){
	var ph = $('#sjyz .shoujiyz > input').val();
	if(!$.checkMobile(ph)){
		$(_en).attr('href','javascript:void(0);');
		xfAlert('手机号码不正确,请重新输入');
		return false;
	}
	$(_en).attr('href','#sjyz_2');
	$('#jieshoudx').unbind("click");
	$('#jieshoudx').click(function(){
		var v = $(this).val();
		if(v=='60秒后可再获取'){
			console.warn('发送~~');
			reSendCode();
		}
	});
	sendCode(ph);
}
//重新接收短信
reSendCode=function(){
	var ph = $('#sjyz .shoujiyz > input').val();
	if(!$.checkMobile(ph)){	//一般在这个地方的时候手机号码是对的
		xfAlert('手机号码不正确,请重新输入');
		return false;
	}
	sendCode(ph);
}
checkCheckingCode= function(_en){
	var code = $('#sjyz_2 .shoujiyz > input').val();
	var ph = $('#sjyz .shoujiyz > input').val();
	if(!code||code==''){
		xfAlert('请输入验证码');
		return false;
	}
	if(checking(ph,code)){
		$(_en).attr('href','#shezhi');
	}else{
		$(_en).attr('href','javascript:void(0);');
	}
}

sendCode = function(ph){
	$.ajax({
		url :interfaceMobileUrl+"/bindPhone/send.json",
		data:{'userid':userid,'key':userkey,'mobile':ph},
		jsonp: "callback",
		dataType: "jsonp",
		error:function(){
			xfAlert('信息发送失败');
		},
		success:function(data){
			if(data=='-1'){
				$('#sjyz_2 .shoujiyz .csdx').html('表示手机格式错误').attr('style','color:red;');
			}else if(data=='-2'){
				$('#sjyz_2 .shoujiyz .csdx').html('手机已经被绑定').attr('style','color:red;');
			}else if(data=='-3'){
				$('#sjyz_2 .shoujiyz .csdx').html('发送失败,请稍后重试').attr('style','color:red;');
			}
		}
	});
}
checking = function(ph,code){
	$.ajax({
		url :interfaceMobileUrl+"/bindPhone/validate.json",
		data:{'userid':userid,'key':userkey,'mobile':ph,'vCode':code},
		jsonp: "callback",
		dataType: "jsonp",
		async: false,
		error:function(){
			xfAlert('信息发送失败');
		},
		success:function(data){
			if(data=='1'){
//				成功后刷新页面
//				var href = location.href;
//				location.href=href.slice(0,href.indexOf('#'));
				personalInfo();
				$.ui.loadContent("#shezhi",false,false,"up");
			}else if(data=='-1'){
				$('#sjyz_2 .shoujiyz .csdx').html('验证码错误').attr('style','color:red;');
			}else if(data=='0'){
				$('#sjyz_2 .shoujiyz .csdx').html('验证码过期').attr('style','color:red;');
			}
		}
	});
	return false;
}
//搜索图书馆
function seachLib(){
	var $ul = $('.tsgssjg').find('ul');
	$ul.empty();
	var q = $('.dzssk').find('input').val();
	if(q&&$.trim(q)!=''){
		//先将之前搜索到的馆信息清空
		$.ajax({
			url : interfaceMobileUrl+'/queryLibs.json',
			data:{'userid':userid,'key':userkey,'query':q},
			jsonp: "callback",
		    dataType: "jsonp",
		    beforeSend:function(){
		    	$ul.html("<li style='background: white;'>查询中...</li>");
		    },
			success:function(da){
				fillData(da,$ul);
			},
			timeout:30000
		});
	}else{
		xfAlert('请输入关键词');
	}
}
//将数据写到对应的节点 
function fillData(data,_dom){
	_dom.html("");
	data = eval('(' + data + ')');
	data = data.list[0]['com.opac.readerHome.domain.Lib'];
	if(!data){
		_dom.append("<li style='background: white;'>找不到图书馆</li>");
	}else if(!data.length){
		_dom.append("<li gLibname='"+data.gLibname+"' gLibcode='"+data.gLibcode+"' >"+data.gLibname+"</li>");
	}else{
		$.each(data,function(i,lib){
			_dom.append("<li gLibname='"+lib.gLibname+"' gLibcode='"+lib.gLibcode+"' >"+lib.gLibname+"</li>");
		});
	}
//	$("#tushuguan div ul li").on("click",function(){$(this).addClass("cur").siblings().removeClass("cur").parent().parent().siblings().find("li").removeClass("cur")});
	$("#bdduzz li").unbind("click");
	$("#bdduzz li").on("click",function(){
		$("#bdduzz li").removeClass("cur");
		$(this).addClass("cur");
	});
}
//绑定读者证
bindRd = function(){
	//所在城市的图书馆
	var cityLib = function(){
		var _dom = $('#tushuguan').find('ul').eq(0);
		_dom.html("");
		$.ajax({
			url : interfaceMobileUrl+'/cityLibrary.json',
			jsonp: "callback",
			dataType: "jsonp",
			data:{'userid':userid,'key':userkey,'city':city},
		    beforeSend:function(data){
		    	$.ui.showMask("加载中...");
			},
			complete:function(){
				$.ui.hideMask();
			},
			error:function(data){
				xfAlert("是不是网络不通啊");
			},
			success:function(da){
				fillData(da,_dom);
			},
			timeout:30000
		});
	};
	//全部图书馆
	var allLib = function(){
		var _alldom = $('#tushuguan').find('ul').eq(1);
		_alldom.html("");
		$.ajax({
			url : interfaceMobileUrl+'/allLibray.json',
			data:{'userid':userid,'key':userkey},
			jsonp: "callback",
		    dataType: "jsonp",
			success:function(da){
				fillData(da,_alldom);
			},
			timeout:30000
		});
	};
	
	
	//绑定点击事件  搜索事件
	
	cityLib();
	allLib();
}
$(function(){
	$("#sendPhoneInput").keydown(function(e){  
//		alert(e.keyCode);   
		if(e && e.keyCode==13){
			$("#sendCode").click();
//			alert("enter");
        }
	});
	$('#bdduzz .dzssk a').on('click',function(){
		seachLib();
	});
	$('#bdduzz .dzssk i').on('click',function(){
		$('.dzssk input').val(" ");
	});
	
	$("a[href='#bdduzz_2']").click(function(){
		var curLi = $("#bdduzz li.cur");
		if(curLi[0] == undefined){
			xfAlert("请选择一个图书馆");
			return false;
		}
		$(".bdtsg2 p").html(curLi.attr("glibname"));
		var gLibcode = curLi.attr("glibcode");
		$("#bindRdidConfirm").unbind("click");
		$("#bindRdidConfirm").click(function(){
			var rdid = $(".bdtsg2 input:text").val();
			var rdPsd = $("#rdPassword").val();
			if(rdid == ""){
				xfAlert("请输入读者证号");
				return false;
			}
			if(rdPsd == ""){
				xfAlert("请输入读者证密码");
				return false;
			}
			$.ajax({
				url :interfaceMobileUrl+"/bindRdid.json?userid="+userid+"&key="+userkey,
			    jsonp: "callback",
				dataType: "jsonp",
			    beforeSend:function(){
			    	 $.ui.showMask("验证中...");
			    },
			    complete:function(){
			    	$.ui.hideMask(); 
			    },
			    data:{
			    	rdid:rdid,
			    	rdpasswd:rdPsd,
			    	gLibcode:gLibcode
			    },
				error:function(){
					xfAlert("访问失败");
				},
				success:function(data){
					if(data=="1"){
						xfAlert("绑定读者证成功");
						personalInfo();
						$.ui.loadContent("#shezhi",false,false,"up");
						return;
					}
					if(data=="-1"){
						xfAlert("一个馆只能绑定一个证");
						personalInfo();
						return;
					}
					if(data=="-2"){
						xfAlert("读者证验证失败");
						personalInfo();
						return;
					}
					if(data=="-3"){
						xfAlert("读者证已被绑定");
						personalInfo();
						return;
					}
				}
			});
				
		});
	});
});

tuichu = function(){
	window.localStorage.removeItem("userid");
	window.localStorage.removeItem("userkey");
	var href = location.href;
	var n = href.indexOf('#');
	n = (n>-1)?n:href.length;
	location.href=href.slice(0,n);
//	window.localStorage.setItem("userid", '');
//	window.localStorage.setItem("userkey", '');
}





